﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_PerAdminDAL : IDcms_PerAdminDAL
    {
        public Dcms_PerAdminModel AddModel(Dcms_PerAdminModel model)
        {
            string sQLString = "insert into Dcms_PerAdmin(PA_AdminId,PA_PId,PA_IsRead,PA_IsAdd,PA_IsEdit,PA_IsDelete) values(@PA_AdminId,@PA_PId,@PA_IsRead,@PA_IsAdd,@PA_IsEdit,@PA_IsDelete) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@PA_AdminId", SqlDbType.Int), new SqlParameter("@PA_PId", SqlDbType.Int), new SqlParameter("@PA_IsRead", SqlDbType.TinyInt), new SqlParameter("@PA_IsAdd", SqlDbType.TinyInt), new SqlParameter("@PA_IsEdit", SqlDbType.TinyInt), new SqlParameter("@PA_IsDelete", SqlDbType.TinyInt) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.PA_AdminId;
            cmdParms[2].Value = model.PA_PId;
            cmdParms[3].Value = model.PA_IsRead;
            cmdParms[4].Value = model.PA_IsAdd;
            cmdParms[5].Value = model.PA_IsEdit;
            cmdParms[6].Value = model.PA_IsDelete;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.PA_Id = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_PerAdminModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_PerAdminModel> list = new List<Dcms_PerAdminModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_PerAdminModel item = new Dcms_PerAdminModel();
                if (ds.Tables[0].Rows[i]["PA_Id"].ToString() != "") item.PA_Id = int.Parse(ds.Tables[0].Rows[i]["PA_Id"].ToString());
                if (ds.Tables[0].Rows[i]["PA_AdminId"].ToString() != "") item.PA_AdminId = int.Parse(ds.Tables[0].Rows[i]["PA_AdminId"].ToString());
                if (ds.Tables[0].Rows[i]["PA_PId"].ToString() != "") item.PA_PId = int.Parse(ds.Tables[0].Rows[i]["PA_PId"].ToString());
                if (ds.Tables[0].Rows[i]["PA_IsRead"].ToString() != "") item.PA_IsRead = int.Parse(ds.Tables[0].Rows[i]["PA_IsRead"].ToString());
                if (ds.Tables[0].Rows[i]["PA_IsAdd"].ToString() != "") item.PA_IsAdd = int.Parse(ds.Tables[0].Rows[i]["PA_IsAdd"].ToString());
                if (ds.Tables[0].Rows[i]["PA_IsEdit"].ToString() != "") item.PA_IsEdit = int.Parse(ds.Tables[0].Rows[i]["PA_IsEdit"].ToString());
                if (ds.Tables[0].Rows[i]["PA_IsDelete"].ToString() != "") item.PA_IsDelete = int.Parse(ds.Tables[0].Rows[i]["PA_IsDelete"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_PerAdmin where PA_Id=@PA_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PA_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public Dcms_PerAdminModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_PerAdmin where PA_Id=@PA_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PA_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_PerAdminModel model = new Dcms_PerAdminModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_PerAdminModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_PerAdmin";
            return SqlHelper.CreateDataSet(sql);
        }

        public Dcms_PerAdminModel Update(Dcms_PerAdminModel model)
        {
            string sQLString = "update Dcms_PerAdmin set PA_AdminId=@PA_AdminId,PA_PId=@PA_PId,PA_IsRead=@PA_IsRead,PA_IsAdd=@PA_IsAdd,PA_IsEdit=@PA_IsEdit,PA_IsDelete=@PA_IsDelete where PA_Id=@PA_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PA_AdminId", SqlDbType.Int), new SqlParameter("@PA_PId", SqlDbType.Int), new SqlParameter("@PA_IsRead", SqlDbType.TinyInt), new SqlParameter("@PA_IsAdd", SqlDbType.TinyInt), new SqlParameter("@PA_IsEdit", SqlDbType.TinyInt), new SqlParameter("@PA_IsDelete", SqlDbType.TinyInt), new SqlParameter("@PA_Id", SqlDbType.Int) };
            cmdParms[0].Value = model.PA_AdminId;
            cmdParms[1].Value = model.PA_PId;
            cmdParms[2].Value = model.PA_IsRead;
            cmdParms[3].Value = model.PA_IsAdd;
            cmdParms[4].Value = model.PA_IsEdit;
            cmdParms[5].Value = model.PA_IsDelete;
            cmdParms[6].Value = model.PA_Id;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

